package com.obrempon.credit.market.sdk.device.http

import com.obrempon.credit.market.request.HttpHelper
import com.obrempon.credit.market.request.IHttpCallback
import com.obrempon.credit.market.utils.log.ObremponLogger
import com.obrempon.module.easyhttp.EasyHttp
import com.obrempon.module.easyhttp.body.JsonRequestBody
import com.obrempon.module.easyhttp.config.IRequestApi
import com.obrempon.module.easyhttp.lifecycle.ApplicationLifecycle
import com.obrempon.module.easyhttp.listener.OnHttpListener
import org.json.JSONObject
import java.util.function.Supplier

class ObremponDevicePostTask(val url: String, val body: String, val taskId: String? = null) :
    Supplier<JSONObject> {

    override fun get(): JSONObject {
        return ASPostSyncTask(url = url, body = body, taskId = taskId).doPostAction()
    }
}

class ASPostSyncTask(val url: String, val body: String, val taskId: String? = null) {

    fun doPostAction(): JSONObject{
        if (this.url.isBlank() or this.body.isBlank()) return JSONObject().apply {
            put("taskId", taskId)
        }

        ObremponLogger.i("Device Url：  $url")
        ObremponLogger.i("Device Params： $body")

        HttpHelper.post(url,JSONObject(body),object : IHttpCallback{
            override fun onRequestSuccess(data: JSONObject) {
            }

            override fun onRequestError(error: JSONObject) {
            }

            override fun onRequestFinish() {
            }
        })

        return JSONObject().apply {
            put("taskId", taskId)
        }
    }
}